﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class CT_DonDH_NguyenVatLieuDAO
    {
        public List<CT_DonDH_NguyenVatLieuDTO> LayDanhSachDonDH_NguyenVatLieuTheoMa(int MaDDH)
        {
            List<CT_DonDH_NguyenVatLieuDTO> listDDHNVL = new List<CT_DonDH_NguyenVatLieuDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select a.*, b.Ten, b.DonViTinh, b.DonGiaBan from CTDonDH_NguyenVatLieu a, NguyenVatLieu_SanPhamGiaiKhat b WHERE a.MaNVL = b.MaNVL AND MaDDH = " + MaDDH;

                conn = DataProvider.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    CT_DonDH_NguyenVatLieuDTO ctddhnvlDTO = new CT_DonDH_NguyenVatLieuDTO();
                    ctddhnvlDTO.MaDDH = reader.GetInt32(0);
                    ctddhnvlDTO.MaNVL = reader.GetInt32(1);
                    ctddhnvlDTO.SoLuong = reader.GetInt32(2);
                    ctddhnvlDTO.Ten = reader.GetString(3);
                    ctddhnvlDTO.DonViTinh = reader.GetString(4);
                    ctddhnvlDTO.DonGiaBan = Convert.ToDouble(reader.GetDecimal(5));
                    listDDHNVL.Add(ctddhnvlDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listDDHNVL;
        }

        public String ThemChiTietDonDH_NguyenVatLieu(CT_DonDH_NguyenVatLieuDTO cthddnvlDTO)
        {
            String mess = "";
            SqlConnection conn = null;
            try
            {
                conn = DataProvider.ConnectDB();
                String sql = "INSERT INTO CTDonDH_NguyenVatLieu(MaDDH,MaNVL,SoLuong) ";
                sql += "VALUES(@MaDDH,@MaNVL,@SoLuong)";
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                SqlParameter para;
                para = cmd.Parameters.Add("@MaDDH", SqlDbType.Int);
                para.Value = cthddnvlDTO.MaDDH;
                para = cmd.Parameters.Add("@MaNVL", SqlDbType.Int);
                para.Value = cthddnvlDTO.MaNVL;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = cthddnvlDTO.SoLuong;

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                mess = "Lỗi thêm chi tiết hóa đơn";
                String e = ex.ToString();
            }
            finally
            {
                conn.Close();
            }
            return mess;
        }
    }
}
